package service

import (
	"ao/app/dao"
	"ao/app/model"
	"ao/app/request"
	commonApi "ao/common/api"
	"github.com/gogf/gf/errors/gerror"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/os/glog"
	"github.com/gogf/gf/text/gstr"
	"github.com/gogf/gf/util/gconv"
	"time"
)

var DeviceInfo = deviceInfoService{}

type deviceInfoService struct {
	commonApi.CommonBase
}

func (s *deviceInfoService) create(req *request.ReportParams) (err error) {
	DeviceInfo := new(model.DeviceInfo)
	DeviceInfo.Speed = req.Speed
	DeviceInfo.Posx = req.PosX
	DeviceInfo.Posy = req.PosY
	DeviceInfo.Posh = req.PosH
	DeviceInfo.GnssTime = req.GnssTime
	DeviceInfo.Direction = req.Direction
	_, err = g.Model("device_info").Data(DeviceReport).Insert()
	glog.Infof("res:%v", err)
	return
}
func (s *deviceInfoService) List(req *request.DeviceReportSearchReq) (pageSize, total, page int, list []*request.Device, err error) {
	model := dao.DeviceInfo.M

	total, err = model.Count()
	if err != nil {
		g.Log().Error(err)
		err = gerror.New("获取总行数失败")
		return
	}
	if req.CurrentPage == 0 {
		req.CurrentPage = 1
	}
	page = req.CurrentPage
	if req.PageSize == 0 {
		req.PageSize = request.PageSize
	}
	pageSize = req.PageSize
	err = model.Page(page, req.PageSize).Order("created_at asc").Scan(&list)
	if err != nil {
		g.Log().Error(err)
		err = gerror.New("获取数据失败")
		return
	}
	return
}
func (s *deviceInfoService) updateKeep(req *request.ReportParams) (err error) {
	DeviceReportDaoColumns := dao.DeviceInfo.Columns
	updateRow := g.Map{}
	lastTime := time.UnixMilli(gconv.Int64(req.GnssTime)).Add(13 * time.Hour).Format(time.RFC3339Nano)
	lastTime = gstr.SubStr(lastTime, 0, len(lastTime)-6) + "+08:00"
	if req.PosX != "" && req.PosY != "" {
		updateRow = g.Map{ //更新数据
			DeviceReportDaoColumns.Posy:      req.PosY,
			DeviceReportDaoColumns.Posx:      req.PosX,
			DeviceReportDaoColumns.Posh:      req.PosH,
			DeviceReportDaoColumns.Lasttime:  lastTime,
			DeviceReportDaoColumns.Onlinetcp: 1,
		}
	} else {
		updateRow = g.Map{ //更新上线
			DeviceReportDaoColumns.Onlinetcp: 1,
		}
	}
	_, err = g.Model("device_info").Where("imei", req.IMEI).Update(updateRow)
	glog.Infof("res:%v", err)
	return
}
func (s *deviceInfoService) updateOnline(req *request.ReportParams) (err error) {
	DeviceReportDaoColumns := dao.DeviceInfo.Columns
	_, err = g.Model("device_info").Where("imei", req.IMEI).Update(g.Map{
		DeviceReportDaoColumns.Lasttime:  time.UnixMilli(gconv.Int64(req.GnssTime)).Add(13 * time.Hour).Format(time.RFC3339Nano),
		DeviceReportDaoColumns.Onlinetcp: 1,
	})
	glog.Infof("res:%v", err)
	return
}
func (s *deviceInfoService) updateOffline(req *request.ReportParams) (err error) {
	DeviceReportDaoColumns := dao.DeviceInfo.Columns
	_, err = g.Model("device_info").Where("imei", req.IMEI).Update(g.Map{
		DeviceReportDaoColumns.Onlinetcp: 0,
	})
	glog.Infof("res:%v", err)
	return
}
func (s *deviceInfoService) GroupList(req *request.DeviceReportSearchReq) (pageSize, total, page int, list []*request.Device, err error) {
	model := dao.DeviceInfo.M
	total, err = model.Count()
	if err != nil {
		g.Log().Error(err)
		err = gerror.New("获取总行数失败")
		return
	}
	if req.CurrentPage == 0 {
		req.CurrentPage = 1
	}
	page = req.CurrentPage
	if req.PageSize == 0 {
		req.PageSize = request.PageSize
	}
	pageSize = req.PageSize
	req2 := req
	req2.PageSize = 10000
	req2.CurrentPage = 1
	err = model.OrderDesc("created_at").Scan(&list)
	if err != nil {
		g.Log().Error(err)
		err = gerror.New("获取数据失败")
		return
	}
	return
}
